<template>
  <div class="list">
    <PageHeader>
      <div style="padding-bottom: 16px;">
        <a-button type="primary" style="margin-right:10px;cursor:default;">今日老用户 {{count_data.total_old_user_num}} 个</a-button>
        <template v-for="(item, index) in count_data.old_user">
          <a-tag :color="item.color" style="margin-right:10px;" >{{item.title}} {{item.ratio}}%</a-tag>
        </template>
        
        <a-button type="primary" style="margin-left:200px;margin-right:10px;cursor:default;">今日交易人数 {{count_data.today_buy_user_num}} 人</a-button>
        <a-button type="primary" style="margin-right:10px;cursor:default;">今日交易笔数 {{count_data.today_buy_order_num}} 笔</a-button>

        <a-button type="primary" style="margin-left:60px;margin-right:10px;cursor:default;">今日实付金额 {{count_data.today_pay_money_price}} 元</a-button>
        <a-button type="primary" style="margin-right:10px;cursor:default;">当月实付金额 {{count_data.month_pay_money_price}} 元</a-button>
      </div>
      <div style="padding-bottom: 16px;">
        <a-button type="primary" style="margin-right:10px;cursor:default;">昨日老用户 {{count_data.yesterday_total_old_user_num}} 个</a-button>
        <template v-for="(item, index) in count_data.yesterday_old_user">
          <a-tag :color="item.color" style="margin-right:10px;" >{{item.title}} {{item.ratio}}%</a-tag>
        </template>
        
        <a-button type="primary" style="margin-left:200px;margin-right:10px;cursor:default;">昨日交易人数 {{count_data.yesterday_today_buy_user_num}} 人</a-button>
        <a-button type="primary" style="margin-right:10px;cursor:default;">昨日交易笔数 {{count_data.yesterday_today_buy_order_num}} 笔</a-button>
      
        <a-button type="primary" style="margin-left:60px;margin-right:10px;cursor:default;">昨日实付金额 {{count_data.yesterday_today_pay_money_price}} 元</a-button>
      </div>
      
      <div style="padding-bottom: 16px;">
        <div class="sort-keys">
        <div class="tab-list" style="margin-bottom: 10px;">
           <div class="item" @click="setUserIds('total_user_num', '')" :class="{actived: user_num_type === 'total_user_num'}">
             今日活跃用户 {{count_data.total_user_num}} 个
           </div>
           <div class="item" @click="setUserIds('user_num_new', count_data.user_num_new_ids)" :class="{actived: user_num_type === 'user_num_new'}">
             新用户 {{count_data.user_num_new}} 个
           </div>
           <div class="item" @click="setUserIds('user_num_ad', count_data.user_num_ad_ids)" :class="{actived: user_num_type === 'user_num_ad'}">
             广告用户 {{count_data.user_num_ad}} 个
           </div>
         </div>
        </div>
      </div>
      

    </PageHeader>

    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <a-input placeholder="输入用户手机号" style="width: 180px;margin-right:10px;" v-model="search_phone" allowClear :size="size"
          @input="onSearch" />

<!--        <span style="margin-left: 20px; margin-right: 10px;">用户等级</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="userType" placeholder="用户等级" allowClear>
          <a-select-option :value="item.value" v-for="item in userTypeList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select> -->
        
        <span style="margin-left: 20px; margin-right: 10px;">活跃时间</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="timeType" placeholder="活跃时间" allowClear>
          <a-select-option :value="item.value" v-for="item in timeTypeList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select>

<!--        <span style="margin-left: 20px; margin-right: 10px;">排序</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="orderBy" placeholder="排序" allowClear>
          <a-select-option :value="item.value" v-for="item in orderByList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select> -->
      </div>
      <!-- <a-button type="primary" icon="plus" @click="handleCreate" v-if="!selectMode">创建{{resourceTypeText}}</a-button> -->
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">

      <a-table-column title="用户" width="120px">
        <template slot-scope="row">
          <div class="user" v-if="row.id">
            <img :src="row.headimg" alt="用户头像">
            <div>
              <div>{{ row.name }}</div>
              <div>{{row.phone}}</div>
            </div>
            <a-tag color="red" style="margin-left:10px;" v-if="row.status === 2">已拉黑</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.is_rb === 1">马甲</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.is_rb2 === 1">主播</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.is_liu === 1">老六</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.is_nopay === 1">禁付</a-tag>
            <a-tag color="blue" style="margin-left:10px;" v-if="row.click_id">微广</a-tag>
          </div>
           <!-- <UserDisplay :info="row.user"></UserDisplay> -->
        </template>
      </a-table-column>
      
      <a-table-column title="用户标签" width="140px" >
        <template slot-scope="row">
          <a-tag :color="item.color" v-for="(item, index) in row.user_tag" style="margin-left:10px;" >{{item.title}}</a-tag>
        </template>
      </a-table-column>
      
      <a-table-column title="微信群" width="80px" >
        <template slot-scope="row">
          <a-tag style="margin-left:10px;" v-for="(item, index) in row.qy_groups">{{item}}</a-tag>
        </template>
      </a-table-column>
      
      <a-table-column
        title="注册日期"
        width="110px"
      >
        <template slot-scope="row">
          <span :style="row.created_at | parseStyle">{{ row.created_at | formatDate }}</span>
        </template>
      </a-table-column>
      
      <a-table-column
        title="最后活跃时间"
        width="110px"
      >
        <template slot-scope="row">
          <span :style="row.last_active_at | parseStyle">{{ row.last_active_at | formatDate }}</span>
        </template>
      </a-table-column>
      <a-table-column title="成长等级" width="110px">
        <template slot-scope="row">
          <span>{{ row.task_level }}级</span>
        </template>
      </a-table-column>
      <a-table-column title="今日实付金额" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="total_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>
      <a-table-column title="今日发起提现" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="tx_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>
      <a-table-column title="今日提货订单红包价值" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="send_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>


    </a-table>

  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  import NumberGroupDisplay from './components/NumberGroupDisplay'

  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"

  const resourceType = 'user-online-stats'
  const resourceTypeText = '消费统计'

  export default {
    components: {
      ISwitch,
      NumberGroupDisplay,
      IDatePicker
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isEdit: false,
        editUuid: '',
        resourceTypeText,
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        list: [],
        count_data: [],
        user_ids: '',
        user_num_type: 'total_user_num',
        page: 1,
        per_page: 10,
        total: 0,
        search_phone: '',
        keyType: 'phone',
        category_id: undefined,
        userType: undefined,
        userTypeList: [
          {
            title: '重点用户',
            value: '3',
          },
          {
            title: '高级用户',
            value: '2',
          },
          {
            title: '普通用户',
            value: '1',
          }
        ],
        timeType: undefined,
        timeTypeList: [
          {
            title: '半小时',
            value: '0.5',
          },
          {
            title: '1小时',
            value: '1',
          },
          {
            title: '3小时',
            value: '3',
          },
          {
            title: '6小时',
            value: '6',
          },
          {
            title: '9小时',
            value: '9',
          },
          {
            title: '24小时',
            value: '24',
          },
          {
            title: '48小时',
            value: '48',
          }
        ],
        orderBy: undefined,
        orderByList: [
          {
            title: '按消费金额降序',
            value: 'pay_money_desc',
          },
          {
            title: '按消费金额升序',
            value: 'pay_money_asc',
          },
          {
            title: '按最后活跃时间降序',
            value: 'last_active_at_desc',
          }
        ],              
        filter: {
          min_day: '',
          max_day: ''
        }
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      category_id() {
        this.refreshData()
      },
      userType() {
        this.refreshData()
      },
      timeType() {
        this.refreshData()
      },
      orderBy() {
        this.refreshData()
      },       
      'filter.min_day'() {
        this.refreshData()
      },
      'filter.max_day'() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          current: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {
      parseStyle (value) {
        if(new Date(value).getTime() >= (new Date().getTime() - 86400*2000) ){
            return `color:red;`
        }
      },
      formatDate (value) {
        return value ? moment(value).fromNow() : ''
        
      }
    },
    created() {
      this.fetchData()
      this.fetchCountData()
      // this.getCategoryList()
    },
    methods: {
      setTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'set_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      unsetTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要取消置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'unset_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.editUuid = ""
        this.isEdit = true
      },
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      setUserIds(user_num_type, user_ids = '') {
        this.user_num_type = user_num_type
        this.user_ids = user_ids
        this.fetchData();
      },
      refreshData() {
        this.page = 1
        // this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.page = 1
        this.fetchData()
        // clearTimeout(this.timeout)
        // this.timeout = setTimeout(() => {
        //   this.fetchData()
        // }, 300)
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            search_phone: this.search_phone,
            user_type: this.userType,
            user_ids: this.user_ids,
            time_type: this.timeType,
            order_by_field: this.orderBy
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })
      },
      fetchCountData() {
        this.$http.get(`/${resourceType}/countData`).then(res => {
          this.count_data = res.data
        })
      },
      getCategoryList() {
        this.$api.emit('category.list', 'article').then(res => {
          this.category = res.data.list
        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }
  
  .sort-keys {
    display: flex;
    align-items: center;
  
    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;  
      .item {
        margin-right: 20px;
        //margin-left: 10px;
        padding: 0px 4px 4px 4px;
        cursor: pointer;
        font-weight: 500;
        //font-size: 16px;
  
        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
          font-weight: bold;
        }
  
        &:hover {
          color: #1890FF;
        }
      }
    }
  
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
